package com.sc.nft.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.MallDaoProfitTemplateDao;
import com.sc.nft.entity.MallDaoProfitTemplate;
import com.sc.nft.entity.dto.MallDaoProfitTemplateAdminPageDTO;
import com.sc.nft.entity.vo.EditMallDaoProfitTemplateVO;
import com.sc.nft.entity.vo.MallDaoProfitTemplateAdminPageVO;
import com.sc.nft.service.MallDaoProfitTemplateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商城分润模版DAO白名单 服务实现类
 * </p>
 *
 * @author ligen
 * @since 2024-01-09
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallDaoProfitTemplateServiceImpl implements MallDaoProfitTemplateService {

    private final MallDaoProfitTemplateDao mallDaoProfitTemplateDao;

    /**
     * 获取分润模版白名单列表
     *
     * @param mallDaoProfitTemplateAdminPageVO
     * @return
     */
    @Override
    public Page<MallDaoProfitTemplateAdminPageDTO> getAdminPage(MallDaoProfitTemplateAdminPageVO mallDaoProfitTemplateAdminPageVO) {
        return mallDaoProfitTemplateDao.getAdminPage(mallDaoProfitTemplateAdminPageVO.getPageNo(), mallDaoProfitTemplateAdminPageVO.getPageSize(), mallDaoProfitTemplateAdminPageVO.getTemplateId(), mallDaoProfitTemplateAdminPageVO.getDaoName(), mallDaoProfitTemplateAdminPageVO.getStatus());
    }

    /**
     * 编辑
     *
     * @param editMallDaoProfitTemplateVO
     */
    @Override
    public void edit(EditMallDaoProfitTemplateVO editMallDaoProfitTemplateVO) {
        if (CollectionUtils.isEmpty(editMallDaoProfitTemplateVO.getDaoIds())) {
            mallDaoProfitTemplateDao.delByDaoIdAndTemplateId(editMallDaoProfitTemplateVO.getMallProfitTemplateId(), editMallDaoProfitTemplateVO.getDaoIds());
        } else {
            List<MallDaoProfitTemplate> templateList = getListByMallProfitTemplateId(editMallDaoProfitTemplateVO.getMallProfitTemplateId());
            List<Long> daoList = templateList.stream().map(mallDaoProfitTemplate -> mallDaoProfitTemplate.getDaoId()).collect(Collectors.toList());
            List<MallDaoProfitTemplate> addTemplateList = Lists.newArrayList();
            List<Long> needDelDaoIds = Lists.newArrayList();
            if (CollectionUtils.isEmpty(daoList)) {
                // 全部都是添加
                for (Long daoId : editMallDaoProfitTemplateVO.getDaoIds()) {
                    MallDaoProfitTemplate mallDaoProfitTemplate = new MallDaoProfitTemplate();
                    mallDaoProfitTemplate.setDaoId(daoId);
                    mallDaoProfitTemplate.setMallProfitTemplateId(editMallDaoProfitTemplateVO.getMallProfitTemplateId());
                    addTemplateList.add(mallDaoProfitTemplate);
                }
            } else {
                // 筛选出需要删除 和需要添加的白名单
                needDelDaoIds = daoList.stream().filter(daoId -> !editMallDaoProfitTemplateVO.getDaoIds().contains(daoId)).collect(Collectors.toList());
                for (Long daoId : editMallDaoProfitTemplateVO.getDaoIds()) {
                    if (!daoList.contains(daoId)) {
                        MallDaoProfitTemplate mallDaoProfitTemplate = new MallDaoProfitTemplate();
                        mallDaoProfitTemplate.setDaoId(daoId);
                        mallDaoProfitTemplate.setMallProfitTemplateId(editMallDaoProfitTemplateVO.getMallProfitTemplateId());
                        addTemplateList.add(mallDaoProfitTemplate);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(needDelDaoIds)) {
                mallDaoProfitTemplateDao.delByDaoIdAndTemplateId(editMallDaoProfitTemplateVO.getMallProfitTemplateId(), needDelDaoIds);
            }
            if (!CollectionUtils.isEmpty(addTemplateList)) {
                mallDaoProfitTemplateDao.saveBatch(addTemplateList);
            }
        }
    }

    /**
     * 获取分润模版白名单
     *
     * @param mallProfitTemplateId
     * @return
     */
    @Override
    public List<MallDaoProfitTemplate> getListByMallProfitTemplateId(Long mallProfitTemplateId) {
        return mallDaoProfitTemplateDao.getList(mallProfitTemplateId);
    }
}
